package com.example.tijianbackend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.tijianbackend.dto.PersonalOrderDTO;
import com.example.tijianbackend.pojo.*;
import com.example.tijianbackend.result.Result;
import com.example.tijianbackend.service.*;
import com.example.tijianbackend.utils.SmsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

/**
 * @author: bong~
 * @description: TODO
 * @date: 2024/6/17 16:06
 * @version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/orders")
@Api(tags ="ordersController")
@CrossOrigin("*")
public class OrdersController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealdetailedService setmealdetailedService;

    @Autowired
    private CheckitemService checkitemService;

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private OrdersService ordersService;

    @ApiOperation("获取订单的套餐和医院信息")
    @GetMapping("/getSmAndHp")
    public Result getSmAndHp(Integer smId, Integer hpId){

        Map<String,Object> map=new HashMap<>();

        Setmeal setmeal=setmealService.getById(smId);
        Hospital hospital=hospitalService.getById(hpId);

        map.put("setmeal",setmeal);
        map.put("hospital",hospital);

        return Result.success(map);
    }




    @ApiOperation("添加体检预约信息")
    @PostMapping("/saveOrders")
    public Result saveOrders(@RequestBody Orders orders) throws ExecutionException, InterruptedException {

        // 判断是否存在未归档订单

        QueryWrapper queryWrapper= (QueryWrapper) new QueryWrapper();
        queryWrapper.eq("userId",orders.getUserId());
        queryWrapper.eq("state",1);
        Orders tmp=ordersService.getOne(queryWrapper);

        if(tmp!=null){
            return Result.error("存在订单未归档，无法预约");
        }


        // 默认初始不归档
        orders.setState(1);

        ordersService.saveOrders(orders);

        SmsUtil.sendMessage("0000");

        return Result.success("订单确认成功！",orders);

    }


    @ApiOperation("获取个人体检预约信息")
    @GetMapping("/person")
    public Result getPersonOrder(String userId){

        List<PersonalOrderDTO> list=ordersService.getPersonalOrder(userId);
        return Result.success(list);

    }


    @ApiOperation("取消订单")
    @GetMapping("/cancel")
    public Result cancelOrder(Integer orderId){

        Orders orders=ordersService.getById(orderId);
        if(orders.getState()==2 || orders.getState()==3){
            return Result.error("已归档或已取消预约");
        }

        ordersService.cancelOrder(orderId);

        return Result.success("取消成功");
    }





}
